package com.kms.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jcraft.jsch.JSchException;
import com.kms.console.JSchConsole;
import com.kms.derby.Connections;
import com.kms.model.MSyspara;
import com.kms.model.SystemParameterBean;
import com.kms.service.SystemparameterService;

public class SSHClinetServlet extends HttpServlet{
	SystemparameterService sysservice ;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		sysservice = (SystemparameterService)Connections.getServiceBean(request, "sysparaService");
		
		HttpSession session = request.getSession();
		
		StringBuffer command = new StringBuffer();
		//windows
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String datetime = sdf.format(new Date());
		List<SystemParameterBean> listsysbean = getListSysSetting(request);
		String host="" ;
		String user ="";
		String pwd = "";
		String dir ="";
		int port =0 ;
		String id = request.getParameter("id");
		MSyspara mssys=new MSyspara();
		if(id !=null && !"null".equals(id)){
			mssys = sysservice.selectmSyspara(Integer.valueOf(id));
			if(mssys!=null){
				host = mssys.getOsip();
				user = mssys.getOsuser();
				pwd = mssys.getOspwd();
				dir = mssys.getDbzipdir();
				port = Integer.valueOf(mssys.getOsport());
			}
		}
		
		String ret="";
		String status = request.getParameter("status1");
		String commands = request.getParameter("commands");
		command.append("ll;");
		if("linuxcommand".equals(status) && commands!=null && !"".equals(commands)){
			command = new StringBuffer();
			command.append(commands);
			
		}
		
		if(status!=null){
			try {
				JSchConsole jc = new JSchConsole();
				ret = jc.sshExecute(host, user, pwd, command.toString(),port);
			} catch (Exception e) {
				ret = e.toString();
			}
		}else{
			ret = "";
		}
		request.setAttribute("dir", dir);
		request.setAttribute("command", ret);
		request.setAttribute("listsysbean", listsysbean);
		request.setAttribute("id", id);
		request.setAttribute("listmgroup",  sysservice.selectAllMGroup());
		request.setAttribute("mssys", mssys);
		request.getRequestDispatcher("sshclient.jsp").forward(request,response); 
		return;
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		sysservice = (SystemparameterService)Connections.getServiceBean(request, "sysparaService");
		
		doGet(request, response);
	}
	
	public List<SystemParameterBean> getListSysSetting(HttpServletRequest request){
		List<SystemParameterBean> listsysbean = new ArrayList<SystemParameterBean>();
		try {
			Connection conn = Connections.getConnection(request);
			String SQL = "SELECT * FROM SYSSETTING";
			PreparedStatement stmt = conn.prepareStatement(SQL);
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				SystemParameterBean sysb = new SystemParameterBean();
				sysb.setId(rs.getInt("Id"));
				sysb.setName(rs.getString("Name"));
				sysb.setType(rs.getString("Type"));
				sysb.setValue(rs.getString("Value"));
				sysb.setEnabled(rs.getString("Enabled"));
				sysb.setDescription(rs.getString("Description"));
				listsysbean.add(sysb);
			}
			
			rs.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return listsysbean;
	}
}
